package com.bt.lms.ui.page.sys.utils;

import javafx.scene.control.ComboBox;

import java.util.Map;
import java.util.Set;

/**
 * 地址工具类
 * @author 徐大永
 */
public class AddressUtils {

    /**
     * 省
     */
    private static final Map<Integer, String> provinceMap = RegionUtils.ProvinceInit();

    /**
     * 市
     */
    private static final Map<Integer, String> regionMap = RegionUtils.regionInit();

    /**
     * 区
     */
    private static final Map<Integer, String> cityMap = RegionUtils.cityInit();


    /**
     * 省市关系
     */
    private static final Map<Integer, Integer> rlProvinceCityMap = RegionUtils.RLProvinceCityInit();

    /**
     * 市区关系
     */
    private static final Map<Integer, Integer> rlCityRegionMap = RegionUtils.RLCityRegionInit();

    /**
     * 三级联动
     *
     * @param oneRegion   省级
     * @param twoRegion   市级
     * @param threeRegion 区级
     */
    public static void ganged(ComboBox<String> oneRegion, ComboBox<String> twoRegion, ComboBox<String> threeRegion) {

        //所有省的编号
        Set<Integer> provinceSet = provinceMap.keySet();
        for (Integer key : provinceSet) {
            //省的名字
            String provinceStr = provinceMap.get(key);
            oneRegion.getItems().add(provinceStr);//添加进一级选项
            oneRegion.setOnAction(event -> {//事件
                twoRegion.getItems().clear();//清楚二级选项
                try {
                    String value = oneRegion.getValue();//获取一级选项的值

                    if (value.equals("北京市")) {//一级选择北京
                        Set<Integer> rlProvinceCitySet = rlProvinceCityMap.keySet();//省市关系Set
                        for (Integer city : rlProvinceCitySet) {///取出每一个市编号
                            Integer province = rlProvinceCityMap.get(city);//得到关系的省
                            if (province == 11) {//如果关系的是北京，就添加进二级中
                                String cityStr = cityMap.get(city);
                                twoRegion.getItems().add(cityStr);
                            }
                        }
                    } else if (value.equals("山东省")) {//一级选择山东
                        Set<Integer> rlProvinceCitySet = rlProvinceCityMap.keySet();//得到省市关系
                        for (Integer city : rlProvinceCitySet) {//遍历市编号
                            Integer province = rlProvinceCityMap.get(city);
                            if (province == 37) {//如果是对应山东的市
                                String cityStr = cityMap.get(city);//拿到市名
                                twoRegion.getItems().add(cityStr);//添加进二级目录
                                twoRegion.setOnAction(event1 -> {//二级目录事件
                                    threeRegion.getItems().clear();//清空三级目录
                                    String twoValue = twoRegion.getValue(); //获取选中的二级目录名称
                                    switch (twoValue) {//对比各个市
                                        case "济南市": {
                                            //取出该市所有区并加入三级目录
                                            Set<Integer> rlCityRegionSet = rlCityRegionMap.keySet();
                                            for (Integer rlRegion : rlCityRegionSet) {
                                                Integer region = rlCityRegionMap.get(rlRegion);
                                                if (region == 1732) {
                                                    String regionStr = regionMap.get(rlRegion);
                                                    threeRegion.getItems().add(regionStr);
                                                }
                                            }
                                            break;
                                        }
                                        case "淄博市": {
                                            Set<Integer> rlCityRegionSet = rlCityRegionMap.keySet();
                                            for (Integer rlRegion : rlCityRegionSet) {
                                                Integer region = rlCityRegionMap.get(rlRegion);
                                                if (region == 1759) {
                                                    String regionStr = regionMap.get(rlRegion);
                                                    threeRegion.getItems().add(regionStr);
                                                }
                                            }
                                            break;
                                        }
                                        case "烟台市": {
                                            Set<Integer> rlCityRegionSet = rlCityRegionMap.keySet();
                                            for (Integer rlRegion : rlCityRegionSet) {
                                                Integer region = rlCityRegionMap.get(rlRegion);
                                                if (region == 1787) {
                                                    String regionStr = regionMap.get(rlRegion);
                                                    threeRegion.getItems().add(regionStr);
                                                }
                                            }
                                            break;
                                        }
                                        case "潍坊市": {
                                            Set<Integer> rlCityRegionSet = rlCityRegionMap.keySet();
                                            for (Integer rlRegion : rlCityRegionSet) {
                                                Integer region = rlCityRegionMap.get(rlRegion);
                                                if (region == 1803) {
                                                    String regionStr = regionMap.get(rlRegion);
                                                    threeRegion.getItems().add(regionStr);
                                                }
                                            }
                                            break;
                                        }
                                        case "泰安市": {
                                            Set<Integer> rlCityRegionSet = rlCityRegionMap.keySet();
                                            for (Integer rlRegion : rlCityRegionSet) {
                                                Integer region = rlCityRegionMap.get(rlRegion);
                                                if (region == 1837) {
                                                    String regionStr = regionMap.get(rlRegion);
                                                    threeRegion.getItems().add(regionStr);
                                                }
                                            }
                                            break;
                                        }
                                    }
                                });
                            }
                        }
                    }
                } catch (Exception ignored) {

                }
            });
        }
    }
}
